/*
 *  Copyright (C) 2011 Joaquín Ignacio Aramendía <samsagax@gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * JPGUI.java
 *
 * Created on 07/01/2011, 20:35:27
 */
package jpercentilos;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import jpercentilos.res.Dimensionizable.*;
import jpercentilos.res.*;
import jpercentilos.res.Length.*;
import jpercentilos.res.PatientProfile.*;
import jpercentilos.res.TablaPercentilos.Tipo;

/**
 *
 * @author Joaquín Ignacio Aramendía <samsagax@gmail.com>
 */
public class JPGUI extends javax.swing.JFrame {

    /** Creates new form JPGUI */
    public JPGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        dateOfBirthField = new javax.swing.JFormattedTextField();
        ageField = new javax.swing.JTextField();
        ageComboBox = new javax.swing.JComboBox();
        heightField = new javax.swing.JTextField();
        heightComboBox = new javax.swing.JComboBox();
        weightField = new javax.swing.JTextField();
        weightComboBox = new javax.swing.JComboBox();
        HPField = new javax.swing.JTextField();
        HPComboBox = new javax.swing.JComboBox();
        sexComboBox = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        javax.swing.JLabel jLabel7 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel8 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel9 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel10 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel16 = new javax.swing.JLabel();
        weightForAgeCentField = new javax.swing.JTextField();
        heightForAgeCentField = new javax.swing.JTextField();
        HPForAgeCentField = new javax.swing.JTextField();
        IMCForAgeCentField = new javax.swing.JTextField();
        weightForHeightCentField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel12 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel13 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel14 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel17 = new javax.swing.JLabel();
        weightForAgePzField = new javax.swing.JTextField();
        heightForAgePzField = new javax.swing.JTextField();
        HPForAgePzField = new javax.swing.JTextField();
        IMCForAgePzField = new javax.swing.JTextField();
        weightForHeightPzField = new javax.swing.JTextField();
        labelWeightForAgeStatus = new javax.swing.JLabel();
        labelHeightForAgeStatus = new javax.swing.JLabel();
        labelHPforAgeStatus = new javax.swing.JLabel();
        labelIMCForAgeStatus = new javax.swing.JLabel();
        labelWeightForHeightStatus = new javax.swing.JLabel();
        IMCOutputField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel15 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JPercentilos (beta)");
        setResizable(false);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del paciente"));

        jLabel1.setText("Fecha de nacimiento:");

        jLabel2.setText("Edad:");

        jLabel3.setText("Talla:");

        jLabel4.setText("Peso:");

        jLabel5.setText("Perímetro cefálico:");

        jLabel11.setText("Sexo:");

        dateOfBirthField.setColumns(10);
        try {
            dateOfBirthField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        dateOfBirthField.setInputVerifier(verificador);

        ageField.setColumns(4);
        ageField.setText("5");
        ageField.setInputVerifier(verificador);

        ageComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "(días)", "(meses)", "(años)" }));
        ageComboBox.setSelectedIndex(1);
        ageComboBox.addActionListener(verificador);

        heightField.setColumns(4);
        heightField.setText("66");
        heightField.setInputVerifier(verificador);

        heightComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "(m)", "(cm)" }));
        heightComboBox.setSelectedIndex(1);

        weightField.setColumns(4);
        weightField.setText("7,3");
        weightField.setInputVerifier(verificador);

        weightComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "(kg)", "(g)" }));

        HPField.setColumns(4);
        HPField.setText("43");
        HPField.setInputVerifier(verificador);

        HPComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "(cm)", "(mm)" }));

        sexComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Varón", "Mujer" }));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(sexComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(HPField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(weightField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(heightField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ageField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(HPComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(weightComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ageComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(heightComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(dateOfBirthField))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dateOfBirthField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ageComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ageField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(heightComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(heightField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(weightComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(weightField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HPField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(HPComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sexComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setAction(new CalculateAction());
        jButton1.setText("Calcular");

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Percentilos"));

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel7.setText("Peso/Edad:");

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel8.setText("Talla/Edad:");

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel9.setText("PC/Edad:");

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel10.setText("IMC/Edad:");

        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel16.setText("Peso/Talla:");

        weightForAgeCentField.setColumns(4);
        weightForAgeCentField.setEditable(false);
        weightForAgeCentField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        heightForAgeCentField.setColumns(4);
        heightForAgeCentField.setEditable(false);
        heightForAgeCentField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        HPForAgeCentField.setColumns(4);
        HPForAgeCentField.setEditable(false);
        HPForAgeCentField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        IMCForAgeCentField.setColumns(4);
        IMCForAgeCentField.setEditable(false);
        IMCForAgeCentField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        weightForHeightCentField.setColumns(4);
        weightForHeightCentField.setEditable(false);
        weightForHeightCentField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel6.setText("z:");

        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel12.setText("z:");

        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel13.setText("z:");

        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel14.setText("z:");

        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel17.setText("z:");

        weightForAgePzField.setColumns(4);
        weightForAgePzField.setEditable(false);
        weightForAgePzField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        heightForAgePzField.setColumns(4);
        heightForAgePzField.setEditable(false);
        heightForAgePzField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        HPForAgePzField.setColumns(4);
        HPForAgePzField.setEditable(false);
        HPForAgePzField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        IMCForAgePzField.setColumns(4);
        IMCForAgePzField.setEditable(false);
        IMCForAgePzField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        weightForHeightPzField.setColumns(4);
        weightForHeightPzField.setEditable(false);
        weightForHeightPzField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        labelWeightForAgeStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelWeightForAgeStatus.setText("SIN DETERMINAR");

        labelHeightForAgeStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelHeightForAgeStatus.setText("SIN DETERMINAR");

        labelHPforAgeStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelHPforAgeStatus.setText("SIN DETERMINAR");

        labelIMCForAgeStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelIMCForAgeStatus.setText("SIN DETERMINAR");

        labelWeightForHeightStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelWeightForHeightStatus.setText("SIN DETERMINAR");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(weightForHeightCentField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(weightForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(heightForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(HPForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(IMCForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel14)
                            .addComponent(jLabel6)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(weightForAgePzField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(heightForAgePzField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(HPForAgePzField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(IMCForAgePzField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(labelHeightForAgeStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
                            .addComponent(labelHPforAgeStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
                            .addComponent(labelIMCForAgeStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
                            .addComponent(labelWeightForAgeStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(weightForHeightPzField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelWeightForHeightStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel10, jLabel16, jLabel7, jLabel8, jLabel9});

        jPanel6Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {HPForAgeCentField, HPForAgePzField, IMCForAgeCentField, IMCForAgePzField, heightForAgeCentField, heightForAgePzField, weightForAgeCentField, weightForAgePzField, weightForHeightCentField, weightForHeightPzField});

        jPanel6Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel12, jLabel13, jLabel14, jLabel17, jLabel6});

        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(weightForAgePzField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelWeightForAgeStatus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(heightForAgePzField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelHeightForAgeStatus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(HPForAgePzField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelHPforAgeStatus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(IMCForAgePzField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelIMCForAgeStatus)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(weightForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(heightForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(HPForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jLabel14)
                            .addComponent(IMCForAgeCentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17)
                    .addComponent(weightForHeightCentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(weightForHeightPzField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelWeightForHeightStatus))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        IMCOutputField.setColumns(6);
        IMCOutputField.setEditable(false);

        jLabel15.setText("IMC:");

        fileMenu.setText("Archivo");

        jMenuItem2.setText("Salir");
        fileMenu.add(jMenuItem2);

        jMenuBar1.add(fileMenu);

        helpMenu.setText("Ayuda");

        jMenuItem1.setText("Acerca de...");
        helpMenu.add(jMenuItem1);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(IMCOutputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jLabel15)
                            .addComponent(IMCOutputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            // Set System L&F
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JPGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox HPComboBox;
    private javax.swing.JTextField HPField;
    private javax.swing.JTextField HPForAgeCentField;
    private javax.swing.JTextField HPForAgePzField;
    private javax.swing.JTextField IMCForAgeCentField;
    private javax.swing.JTextField IMCForAgePzField;
    private javax.swing.JTextField IMCOutputField;
    private javax.swing.JComboBox ageComboBox;
    private javax.swing.JTextField ageField;
    private javax.swing.JFormattedTextField dateOfBirthField;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JComboBox heightComboBox;
    private javax.swing.JTextField heightField;
    private javax.swing.JTextField heightForAgeCentField;
    private javax.swing.JTextField heightForAgePzField;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel labelHPforAgeStatus;
    private javax.swing.JLabel labelHeightForAgeStatus;
    private javax.swing.JLabel labelIMCForAgeStatus;
    private javax.swing.JLabel labelWeightForAgeStatus;
    private javax.swing.JLabel labelWeightForHeightStatus;
    private javax.swing.JComboBox sexComboBox;
    private javax.swing.JComboBox weightComboBox;
    private javax.swing.JTextField weightField;
    private javax.swing.JTextField weightForAgeCentField;
    private javax.swing.JTextField weightForAgePzField;
    private javax.swing.JTextField weightForHeightCentField;
    private javax.swing.JTextField weightForHeightPzField;
    // End of variables declaration//GEN-END:variables
    private static final DecimalFormat doubleFormat = initDoubleFormat();
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); //initDateFormat();
    private Verifier verificador = new Verifier();

    /**
     * Initializes doubleFormat. Set fraction digits to 3 and does not use
     * grouping
     * @return DoubleFormat
     */
    private static DecimalFormat initDoubleFormat() {
        DecimalFormat df = (DecimalFormat) DecimalFormat.getNumberInstance();
        df.setMaximumFractionDigits(3);
        df.setGroupingUsed(false);
        return df;
    }

    private Sexo getSexo() {
        Sexo sexo;
        switch (sexComboBox.getSelectedIndex()) {
            case 0:
                sexo = Patient.Sexo.VARON;
                break;
            case 1:
                sexo = Patient.Sexo.MUJER;
                break;
            default:
                sexo = Patient.Sexo.VARON;
        }
        return sexo;
    }

    /**
     * Reads ageField and ageComboBox to create an Age object.
     * @return
     */
    private Age readAge() {
        try {
            double res = doubleFormat.parse(ageField.getText().replaceAll("\\.", ",")).doubleValue();
            AgeUnit unit = readAgeUnit();
            System.out.println("Age Read: " + res + " " + unit.toString());
            return new Age(res, unit);
        } catch (ParseException pe) {
            return Age.NA;
        }
    }

    private AgeUnit readAgeUnit() {
        AgeUnit unit;
        switch (ageComboBox.getSelectedIndex()) {
            case 0:
                unit = AgeUnit.DÍA;
                break;
            case 1:
                unit = AgeUnit.MES;
                break;
            case 2:
                unit = AgeUnit.AÑO;
                break;
            default:
                unit = AgeUnit.AÑO;
        }
        return unit;
    }

    private Weight getWeight() {
        try {
            double kg = doubleFormat.parse(weightField.getText().replaceAll("\\.", ",")).doubleValue();
            WeightUnit unit;
            switch (weightComboBox.getSelectedIndex()) {
                case 0:
                    unit = WeightUnit.KG;
                    break;
                case 1:
                    unit = WeightUnit.G;
                    break;
                default:
                    unit = WeightUnit.KG;
            }
            System.out.println("Weight read: " + kg + " " + unit.toString());
            return new Weight(kg, unit);
        } catch (ParseException pe) {
            return Weight.NA;
        }
    }

    private Height readHeight() {
        try {
            double cm = doubleFormat.parse(heightField.getText().replaceAll("\\.", ",")).doubleValue();
            LengthUnit unit;
            switch (heightComboBox.getSelectedIndex()) {
                case 0:
                    unit = LengthUnit.M;
                    break;
                case 1:
                    unit = LengthUnit.CM;
                    break;
                default:
                    unit = LengthUnit.M;
            }
            System.out.println("Height read: " + cm + " " + unit.toString());
            return new Height(cm, unit);
        } catch (ParseException pe) {
            return (Height) Height.NA;
        }
    }

    private HeadPerimeter getHeadPerimeter() {
        try {
            double cm = doubleFormat.parse(HPField.getText().replaceAll("\\.", ",")).doubleValue();
            LengthUnit unit;
            switch (HPComboBox.getSelectedIndex()) {
                case 0:
                    unit = LengthUnit.CM;
                    break;
                case 1:
                    unit = LengthUnit.MM;
                    break;
                default:
                    unit = LengthUnit.CM;
            }
            System.out.println("Head Perimeter read: " + cm + " " + unit.toString());
            return new HeadPerimeter(cm, unit);
        } catch (ParseException pe) {
            return (HeadPerimeter) HeadPerimeter.NA;
        }
    }

    private void syncronizeAge() {
        try {
            Age age = new Age(dateFormat.parse(dateOfBirthField.getText()));
            String s = doubleFormat.format(age.getValueInUnit(readAgeUnit()));
            ageField.setText(s);
        } catch (ParseException parseException) {
            parseException.printStackTrace();
        } catch (InvalidUnitException invalidUnitException) {
            invalidUnitException.printStackTrace();
        }
        syncronizeHP();
    }

    private void syncronizeDate() {
        Age age = readAge();
        dateOfBirthField.setText(dateFormat.format(age.getBirthDate()));
        syncronizeHP();
    }

    private void syncronizeHP() {
        Age age = readAge();
        try {
            if (age.getValueInUnit(AgeUnit.AÑO) <= 5) {
                HPField.setEnabled(true);
                HPComboBox.setEnabled(true);
            } else {
                HPField.setEnabled(false);
                HPComboBox.setEnabled(false);
            }
        } catch (InvalidUnitException iue) {
            HPField.setEnabled(true);
            HPComboBox.setEnabled(true);
        }
    }

    /**
     * Validador de datos de entrada y formato. También se encarga de la
     * actualización del GUI
     */
    private class Verifier extends InputVerifier implements FocusListener, ActionListener {

        /**
         * Si el componente seleccionado es un campo de texto, selecciona todo
         * su contenido
         * @param e
         */
        public void focusGained(FocusEvent e) {
            if (e.getComponent() instanceof JTextField) {
                JTextField field = (JTextField) e.getComponent();
                field.selectAll();
            }
        }

        public void focusLost(FocusEvent e) {
        }

        @Override
        public boolean shouldYieldFocus(JComponent input) {
            boolean checked = verify(input);
            showPretty(input);
            if (!checked) {
                Toolkit.getDefaultToolkit().beep();
                focusGained(new FocusEvent(input, FocusEvent.FOCUS_GAINED));
            }
            return checked;
        }

        @Override
        public boolean verify(JComponent input) {
            if (input instanceof JTextField) {
                JTextField field = (JTextField) input;
                return checkField(field, false);
            }
            return false; //No debería pasar
        }

        private void showPretty(JComponent input) {
            if (input instanceof JTextField) {
                JTextField field = (JTextField) input;
                checkField(field, true);
            }
        }

        private boolean checkField(JTextField field, boolean setIt) {
            if (field.equals(ageField)) {
                return checkAgeField(field, setIt);
            } else if (field.equals(heightField)) {
                return checkHeightField(field, setIt);
            } else if (field.equals(weightField)) {
                return checkWeightField(field, setIt);
            } else if (field.equals(HPField)) {
                return checkHPField(field, setIt);
            } else if (field.equals(dateOfBirthField)) {
                return checkDateOfBirthField(field, setIt);
            } else {
                return false; // No debería pasar.
            }
        }

        /**
         * Verifica si el campo de texto contiene un número
         * @param field
         * @return
         */
        private boolean checkForDouble(JTextField field) {
            try {
                doubleFormat.parse(field.getText().replaceAll("\\.", ","));
                return true;
            } catch (ParseException pe) {
                return false;
            }
        }

        /**
         * Checkea el campo de edad. Debe estar entre 0 y 19 años.
         * @param field
         * @param setIt
         * @return
         */
        private boolean checkAgeField(JTextField field, boolean setIt) {
            boolean valid = false;
            final int MIN_AGE = 0;
            final int MAX_AGE = 19;
            double value = MIN_AGE;
            Age a = readAge();
            if (!a.equals(Age.NA)) {
                try {
                    value = a.getValueInUnit(AgeUnit.AÑO);
                    if ((value >= MIN_AGE) && (value <= MAX_AGE)) {
                        valid = true;
                    } else {
                        valid = false;
                    }
                } catch (InvalidUnitException ex) {
//                    Logger.getLogger(JPGUI.class.getName()).log(Level.SEVERE, null, ex);
                    valid = false; //Should not happen
                }
            }
            if (setIt) {
                String s;
                if (!valid) {
                    if (value > MAX_AGE) {
                        a = new Age(MAX_AGE, AgeUnit.AÑO);
                    } else {
                        a = new Age(MIN_AGE, AgeUnit.AÑO);
                    }
                    s = doubleFormat.format(a.getValue());
                    ageComboBox.setSelectedIndex(2);
                } else {
                    s = doubleFormat.format(a.getValue());
                }
                field.setText(s);
                syncronizeDate();
            }
            return valid;
        }

        /**
         * Checkea el campo de Altura, el valore debe ser número positivo.
         * @param field
         * @param setIt
         * @return
         */
        private boolean checkHeightField(JTextField field, boolean setIt) {
            boolean valid = false;
            final int MIN = 0;
            double value = MIN;
            Height h = readHeight();
            if (checkForDouble(field)) {
                try {
                    value = h.getValueInUnit(LengthUnit.CM);
                    if ((value >= MIN)) {
                        valid = true;
                    } else {
                        valid = false;
                    }
                } catch (InvalidUnitException ex) {
                    valid = false;
                }
            }
//            if (setIt) {
//                if (!valid) {
//                    value = MIN;
//                } else {
//                    try {
//                        value = doubleFormat.parse(field.getText()).doubleValue();
//                    } catch (ParseException ex) {
//                        Logger.getLogger(JPGUI.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                }
//                field.setText(doubleFormat.format(value));
//            }
            return valid;
        }

        /**
         * Checkea el campo de Peso, el valor debe ser un número positivo.
         * @param field
         * @param setIt
         * @return
         */
        private boolean checkWeightField(JTextField field, boolean setIt) {
            boolean valid = false;
            final int MIN = 0;
            double value = MIN;
            Weight w = getWeight();
            if (checkForDouble(field)) {
                try {
                    value = w.getValueInUnit(WeightUnit.KG);
                    if (value >= MIN) {
                        valid = true;
                    } else {
                        valid = false;
                    }
                } catch (InvalidUnitException ex) {
                    return false;
                }
            }
//            if (setIt) {
//                if (!valid) {
//                    value = MIN;
//                } else {
//                    try {
//                        value = doubleFormat.parse(field.getText()).doubleValue();
//                    } catch (ParseException ex) {
//                        Logger.getLogger(JPGUI.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                }
//                field.setText(doubleFormat.format(value));
//            }
            return valid;
        }

        /**
         * Checkea el campo de Peso, el valor debe ser un número positivo.
         * @param field
         * @param setIt
         * @return
         */
        private boolean checkHPField(JTextField field, boolean setIt) {
            boolean valid = false;
            final int MIN = 0;
            double value = MIN;
            HeadPerimeter hp = getHeadPerimeter();
            if (hp.equals(HeadPerimeter.NA)) {
                value = -1;
                valid = true;
            } else {
                try {
                    value = hp.getValueInUnit(LengthUnit.CM);
                    if (value >= MIN) {
                        valid = true;
                    } else {
                        valid = false;
                    }
                } catch (InvalidUnitException ex) {
                    return false;
                }
            }
            if (setIt) {
                String s;
                if (!valid) {
                    value = MIN;
                    s = doubleFormat.format(value);
                } else {
                    if (hp.getValue() == -1) {
                        s = "--";
                    } else {
                        s = doubleFormat.format(value);
//                        HPComboBox.setSelectedIndex();
                    }
                }
                field.setText(s);
            }
            return valid;
        }

        /**
         * Chechea el campo de Fecha de nacimiento. Debe ser una fecha válida y
         * en días ser menor a 5 años. Cambia el campo de Edad para que
         * concuerde.
         * @param field
         * @param setIt
         * @return
         */
        private boolean checkDateOfBirthField(JTextField field, boolean setIt) {
            boolean valid = false;
            double years = 0;
            Date now = Calendar.getInstance().getTime();
            Date birth = now;
            double MAX;
            try {
                MAX = (long) new Age(19, AgeUnit.AÑO).getValueInUnit(AgeUnit.AÑO);
            } catch (InvalidUnitException ex) {
                ex.printStackTrace();
                return false; //Should not happen
            }
            try {
                birth = dateFormat.parse(field.getText());
                years = new Age(birth).getValueInUnit(AgeUnit.AÑO);
                if ((birth.before(now) || birth.equals(now)) && (years <= MAX)) {
                    valid = true;
                }
            } catch (Exception e) {
                valid = false;
                years = 0;
            }
            if (setIt) {
                if (!valid) {
                    birth = now;
                    years = 0;
                } else {
                }
                field.setText(dateFormat.format(birth));
                syncronizeAge();
            }
            return valid;
        }

        @Deprecated
        private Date daysDifference(long days) {
            Date date = Calendar.getInstance().getTime();
            date.setTime(date.getTime() - days * (1000 * 60 * 60 * 24));
            return date;
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JComboBox) {
                JComboBox jComboBox = (JComboBox) e.getSource();
                if (jComboBox == ageComboBox) {
                    syncronizeAge();
                }
            }
        }
    }

    /**
     * Acción que calcula los percentilos. Crea un objeto Patient con los datos
     * asignados. Imprime en el IGU los datos obtenidos.
     */
    private class CalculateAction extends AbstractAction {

        /**
         * Lee los datos correspondientes. Imprime los percentilos.
         * @param e
         */
        public void actionPerformed(ActionEvent e) {
//            System.out.println("Calculando..."); //Línea dummy, debería comentarse
            // Llenado de datos
            Patient p = createPatient();
            System.out.println("Patient creado");
            CalcHelper ch = new CalcHelper(p);
            showResults(ch);
        }

        private Patient createPatient() {
            Sexo sexo = getSexo();
            Age age = readAge();
            Weight weight = getWeight();
            Height height = readHeight();
            HeadPerimeter headPerimeter = getHeadPerimeter();
            return new Patient(sexo, age, height, headPerimeter, weight);
        }

        /**
         * Muestra en el IGU los resultados obtenidos de los parámetros de
         * entrada.
         * @param ch
         */
        private void showResults(CalcHelper ch) {
            showOutputString(ch.getIMCString(), IMCOutputField);
            for (Tipo tipo : Tipo.values()) {
                showOutputString(ch.getCentileStringForType(tipo), getOutputCentileField(tipo));
                showStatusLabel(ch.getStatusStringForType(tipo), getStatusLabel(tipo));
                showOutputString(ch.getZScoreStringForType(tipo), getOutputZScoreField(tipo));
            }
        }

        private JTextField getOutputCentileField(Tipo tipo) {
            if (tipo == TablaPercentilos.Tipo.TALLA_A_EDAD) {
                return heightForAgeCentField;
            } else if (tipo == TablaPercentilos.Tipo.PESO_A_EDAD) {
                return weightForAgeCentField;
            } else if (tipo == TablaPercentilos.Tipo.PC_A_EDAD) {
                return HPForAgeCentField;
            } else if (tipo == TablaPercentilos.Tipo.IMC_A_EDAD) {
                return IMCForAgeCentField;
            } else if (tipo == TablaPercentilos.Tipo.PESO_A_TALLA) {
                return weightForHeightCentField; 
            } else {
                return null; // Should not happend
            }
        }

        private JLabel getStatusLabel(Tipo tipo) {
            if (tipo == TablaPercentilos.Tipo.TALLA_A_EDAD) {
                return labelHeightForAgeStatus;
            } else if (tipo == TablaPercentilos.Tipo.PESO_A_EDAD) {
                return labelWeightForAgeStatus;
            } else if (tipo == TablaPercentilos.Tipo.PC_A_EDAD) {
                return labelHPforAgeStatus;
            } else if (tipo == TablaPercentilos.Tipo.IMC_A_EDAD) {
                return labelIMCForAgeStatus;
            } else if (tipo == TablaPercentilos.Tipo.PESO_A_TALLA) {
                return labelWeightForHeightStatus;
            } else {
                return null; // Should not happend
            }
        }

        private JTextField getOutputZScoreField(Tipo tipo) {
            if (tipo == TablaPercentilos.Tipo.TALLA_A_EDAD) {
                return heightForAgePzField;
            } else if (tipo == TablaPercentilos.Tipo.PESO_A_EDAD) {
                return weightForAgePzField;
            } else if (tipo == TablaPercentilos.Tipo.PC_A_EDAD) {
                return HPForAgePzField;
            } else if (tipo == TablaPercentilos.Tipo.IMC_A_EDAD) {
                return IMCForAgePzField;
            } else if (tipo == TablaPercentilos.Tipo.PESO_A_TALLA) {
                return weightForHeightPzField;
            } else {
                return null; // Should not happend
            }
        }

        private void showOutputString(String s, JTextField field) {
            field.setText(s);
        }

        private void showStatusLabel(String s, JLabel label) {
            label.setText(s);
        }
    }
}
